<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>定时计划列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>

<div style="position: relative;height: 100%;margin-top: 10px;" id="jobContentBody">
    <table class="layui-hide" id="jobListTable" lay-filter="jobListTable"></table>
    <!--隐藏域属性配置 -->
    <input id="isStandby" name="isStandby" type="hidden" value="" />
    <script type="text/html" id="jobToolBarTop">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addJob"><i class="layui-icon layui-icon-add-1"></i>添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="runJob" id="runJob"><i class="iconfont iconplaycircle"></i> 启动定时器</button>
            <button class="layui-btn layui-btn-sm" lay-event="stopJob" id="stopJob"><i class="iconfont iconpausecircle"></i> 停止定时器</button>
        </div>
    </script>
</div>

<script type="text/html" id="jobToolBar">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delJob">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="addTrigger">添加计划</a>
    <a class="layui-btn layui-btn-xs" lay-event="triggerList">计划列表</a>
    <a class="layui-btn layui-btn-xs" lay-event="run">执行</a>
    <a class="layui-btn layui-btn-xs" lay-event="jobLog">日志</a>
</script>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table','layer','setter', 'commons'],function(){
        var table = layui.table,setter = layui.setter,$ = layui.jquery,layer = layui.layer,commons = layui.commons;
        table.render({
            elem:"#jobListTable"
            ,url:serverPath.systemPath + setter.sysLinkPath+"scheduler/jobList"
            ,cellMinWith:setter.cellMinWidth
            ,defaultToolbar: ['filter', 'print']
            ,toolbar: '#jobToolBarTop'
            // ,page:true
            ,cols:[[
                {type:'numbers',align:'center', title: '序号'}
                ,{field:'name', title: '任务名称'}
                ,{field:'jobClass', title: '任务类'}
                ,{field:'description', title: '任务描述'}
                ,{title: '管理',align:'center',toolbar:'#jobToolBar'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(jobListTable)', function(obj){
            switch(obj.event){
                case 'addJob':
                    location.href= serverPath.systemPath+setter.sysLinkPath +"scheduler/view/addJob";
                    break;
                case 'runJob':
                    changeStart();
                    break;
                case 'stopJob':
                    changeStart();
                    break;
            };
        });
        changeButtonStopOrStart('${isStandby}');
        //启动|停止任务
        function changeStart(){
            $.ajax({
                async:false,
                url: serverPath.systemPath + setter.sysLinkPath + "scheduler/changeStart?isStandby="+$("#isStandby").val(),
                type: "get",
                dataType: 'json',
                success: function(data) {
                    if (commons._filterRecord(data)) {
                        var message = data.msg;
                        layer.alert(message);
                        // table.reload('jobListTable', {});
                        //因layui不刷新页面 不会调用controller方法查询任务状态，暂时使用js方法。。。
                        if(message.search("启动")>-1){
                            //启动成功后isStandby=false
                            changeButtonStopOrStart('false');
                        }else{
                            //停止成功后isStandby=true
                            changeButtonStopOrStart('true');
                        }
                    } else {
                        layer.alert(data.msg);
                    }
                    // location.href=serverPath.systemPath + "index#/" + setter.sysLinkPath +"scheduler/view/jobList";
                }
            });
        }

        //切换 启动|停止定时器按钮
        function changeButtonStopOrStart(isStandby){
            if('true'==isStandby){
                $("#runJob").show();
                $("#stopJob").hide();
                $("#isStandby").val(isStandby);
            }else{
                $("#runJob").hide();
                $("#stopJob").show();
                $("#isStandby").val(isStandby);
            }
        }

        //监听行工具栏
        table.on('tool(jobListTable)',function(obj){
            var data = obj.data;
            switch(obj.event){
                case "addTrigger":
                    $(this).attr("href", serverPath.systemPath+ setter.sysLinkPath +"scheduler/view/addTrigger?jobName=" + data.name);
                    break;
                case "triggerList":
                    $(this).attr("href", serverPath.systemPath+ setter.sysLinkPath +"scheduler/view/triggerList?jobName="+obj.data.name);
                    break;
                case "delJob":
                    layer.confirm('确定删除当前数据吗？', function() {
                        $.ajax({
                            url: serverPath.systemPath + setter.sysLinkPath + "scheduler/delJob?jobName="+data.name,
                            type: "get",
                            dataType: 'json',
                            success: function(data) {
                                if (commons._filterRecord(data)) {
                                    layer.alert(data.msg);
                                    table.reload('jobListTable', {});
                                    changeButtonStopOrStart($("#isStandby").val());
                                } else {
                                    layer.alert(data.msg);
                                }
                            }
                        });
                    });
                    break;
                case "run":
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + "scheduler/executeJob?jobName="+data.name,
                        type: "get",
                        dataType: 'json',
                        success: function(data) {
                            if (commons._filterRecord(data)) {
                                layer.alert(data.msg);
                            } else {
                                layer.alert(data.msg);
                            }
                        }
                    });
                    break;
                case "jobLog":
                    $(this).attr("href", serverPath.systemPath+ setter.sysLinkPath +"sysJobLog/view/list?jobName="+obj.data.name+"&baseUrl="+serverPath.systemPath + setter.sysLinkPath+"scheduler/view/jobList");
                    break;
            }
        });
    });
</script>
</body>
</html>